package com.example.yuejian.yuejian.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import com.example.yuejian.yuejian.R;
import com.nineoldandroids.view.ViewHelper;

/**
 * Created by Administrator on 2017/9/12.
 */

public class SlidingMenu extends HorizontalScrollView {
    private LinearLayout mWapper;
    private ViewGroup mMenu;
    private ViewGroup mContent;
    private  int mScreenWidth;
    private  int mMenuRightPadding=50;
    private boolean once;
    private int mMenuWidth;
    private boolean isOpen;

    //未使用自定的属性是调用
    public SlidingMenu(Context context, AttributeSet attrs) {
        this(context,attrs,0);
    }

    //当使用自定义属性的的调用次构造方法


    public SlidingMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取我们的属性
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SlidingMenu,defStyleAttr,0);

        int n=a.getIndexCount();
        for (int i =0 ;i<n;i++)
        {
            int attr =a.getIndex(i);
            switch (attr)
            {
                case  R.styleable.SlidingMenu_rightPadding:
                    mMenuRightPadding=a.getDimensionPixelSize(attr,
                            (int) TypedValue.applyDimension(
                                    TypedValue.COMPLEX_UNIT_DIP,50,context.getResources().getDisplayMetrics()));
                    break;

            }
        }

        a.recycle();
        //获取手机宽度
        WindowManager wm= (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outmetrics= new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outmetrics);
        mScreenWidth=outmetrics.widthPixels;
       /* //把DP转化位Px
        mMenuRightPadding= (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,50,context.getResources().getDisplayMetrics());
*/
    }

    public SlidingMenu(Context context) {
        this(context,null);

    }
    //设置子View的宽度和高度
    //设置自己的宽和高

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        if (!once)
        {
            mWapper= (LinearLayout) getChildAt(0);
            mMenu= (ViewGroup) mWapper.getChildAt(0);
            mContent= (ViewGroup) mWapper.getChildAt(1);
            mMenuWidth= mMenu.getLayoutParams().width=mScreenWidth-mMenuRightPadding;
            mContent.getLayoutParams().width=mScreenWidth;
            once=true;
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    //通过设置偏移量进行隐藏
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (changed)
        {
            this.scrollTo(mMenuWidth,0);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {


        int action=ev.getAction();
        switch(action)
        {
            case MotionEvent.ACTION_UP:
                //隐藏在左边的宽度
                int scrollX=getScrollX();
                if (scrollX>=mMenuWidth/2)
                {
                    this.smoothScrollTo(mMenuWidth,0);
                    isOpen=false;
                }
                else
                {
                    this.smoothScrollTo(0,0);
                    isOpen=true;
                }
                return true;

        }
        return super.onTouchEvent(ev);
    }
    public void openMenu()
    {
        if (isOpen)return;
        this.smoothScrollTo(0,0);
        isOpen=true;
    }
    public  void closeMenu()
    {
        if(!isOpen)return;;
        this.smoothScrollTo(mMenuWidth,0);
        isOpen=false;
    }
    //切换菜单
    public  void toggle()
    {
        if (isOpen)
        {
            closeMenu();
        }
        else
        {
            openMenu();
        }
    }
    /**
     * 滚动发生时
     */

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        float scale= l * 1.0f / mMenuWidth;//1~0


        /**
         * 区别1：内荣区域1.0~0.7缩放的效果
         * scale :1.0~0.0
         * 0.7+0.3*scal
         * q区别2：菜单的偏移量与需要gaibian
         * 区别三：菜单的显示时有缩放以及透明度的变化
         * 缩放：0.7~1.0
         * 1.0-scale*0.3
         * 透明度0.6——1.0
         * 0.6+0.4*（1-scale）
         */
        float rightScale=0.7f+0.3f * scale;

        float leftScale=1.0f-scale*0.3f;
        float leftAlpha=0.6f+0.4f *(1-scale);
        //调用动画属性，设置TranslationX
        ViewHelper.setTranslationX(mMenu,mMenuWidth*scale*0.8f);
        ViewHelper.setScaleX(mMenu,leftScale);
        ViewHelper.setScaleY(mMenu,leftScale);
        ViewHelper.setAlpha(mMenu,leftAlpha);


        //设置content的缩放中心点
        ViewHelper.setPivotX(mContent,0);
        ViewHelper.setPivotY(mContent,mContent.getHeight()/2);


        ViewHelper.setScaleX(mContent,rightScale);
        ViewHelper.setScaleY(mContent,rightScale);
    }




}
